<?php
/**
 * @Author:      lyt
 * @DateTime:    2016-01-26 10:08:38
 * @Description: 用于记录订单的出库记录
 */
class MaintenanceOutOrder extends Eloquent
{
    protected $table = "maintenance_out_order";
    /**
     * @Author:      zwh
     * @DateTime:    2016-1-29 14:14:10
     * @Description: 更新备货记录状态
     */
    public static function updateMaintenanceOutOrderStatus($ids,$order_id){
        return DB::transaction(function () use ($ids,$order_id) {
            foreach($ids as $id){
                $moo=MaintenanceOutOrder::find($id);
                if(is_null($moo)){
                    DB::rollback();
                    return Lang::get("scs_error.order_out_error_40020");
                }
                if($moo->order_id !=$order_id){
                    DB::rollback();
                    return Lang::get("scs_error.order_product_status_error_40009");
                }
                if($moo->status!=1){
                    DB::rollback();
                    // if($moo->type==0){
                    //     $si=SupplyItem::find($moo->item_id);
                    //     $ss=SupplySeries::find($si->series_id);
                    //     $name=$ss->name.' '.$si->model;
                    // }else{
                    //     $si=SupplyItemNonStandard::find($moo->item_id);
                    //     $name=$si->name;
                    // }
                    $item_info=SupplyItem::getCacheItemInfo($moo->item_id);
                    $name=$item_info->name;
                    $response=Lang::get("scs_error.maintenance_out_order_status_error_40021");
                    $response["result"]=array($name."出库单记录状态错误");
                    return $response;
                }
                $moo->status=2;
                $moo->save();
            }
            return array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_id));
        });
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-03-07 15:32:54
     * @Description: 用于拒绝订单 出库的申请 
     */
    public static function refuseOrder($out_orders){
        return DB::transaction(function () use ($out_orders) {            
            foreach ($out_orders as $_oo) {                
                //更改 出库申请 为删除
                $_oo->status=4;
                $_oo->save();
            }                    
        });
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-05-19 19:05:05
     * @Description: 传入订单号检测该订单是否已经全部 领用
     */
    public static function isAllOut($order_id){
        $order_product=OrderProduct::where("order_id",$order_id)->where("status",1)->get();
        foreach ($order_product as $_op) {
            switch ($_op->product_type) {
                case 'enterprise':
                    $opep=OrderProductEsPackage::with("item")->where("order_product_id",$_op->id)->get();
                    foreach ($opep as $key => $value){
                        foreach ($value->item as $_item) {
                            if($_item->type==1){
                                $moo=MaintenanceOutOrder::where("product_type","enterprise")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->get();//查看有没有       备货中 等待领料 或者 已经领料的 备货申请 如果有 说明已经备货
                                if(sizeof($moo)<=0){
                                    return false;
                                }
                                $out_num=0;
                                foreach($moo as $_moo){
                                    $out_num+=$_moo->number-$_moo->return_number;
                                }
                                if(bccomp($out_num,$_item->num,2)!=0){
                                    return false;
                                }  
                            }
                        }
                    }
                    break;
                case 'activity':
                    $opacp=OrderProductAcPackage::with("item")->where("order_product_id",$_op->id)->get();
                    foreach ($opacp as $key => $value){
                        foreach ($value->item as $_item) {
                            if($_item->type==1){
                                $moo=MaintenanceOutOrder::where("product_type","activity")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->get();//查看有没有       备货中 等待领料 或者 已经领料的 备货申请 如果有 说明已经备货
                                if(sizeof($moo)<=0){
                                    return false;
                                }
                                $out_num=0;
                                foreach($moo as $_moo){
                                    $out_num+=$_moo->number-$_moo->return_number;
                                }
                
                                if(bccomp($out_num,$_item->num,2)!=0){
                                    return false;
                                }  
                            }
                        }
                    }
                    break;
                case 'promotion':
                    $opp=OrderProductPackage::with("item")->where("order_product_id",$_op->id)->get();
                    foreach ($opp as $key => $value){
                        foreach ($value->item as $_item) {
                            if($_item->type==1){
                                $moo=MaintenanceOutOrder::where("product_type","promotion")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->get();//查看有没有       备货中 等待领料 或者 已经领料的 备货申请 如果有 说明已经备货                        
                                if(sizeof($moo)<=0){
                                    return false;
                                }
                                $out_num=0;
                                foreach($moo as $_moo){
                                    $out_num+=$_moo->number-$_moo->return_number;
                                }
                                if(bccomp($out_num,$_item->num,2)!=0){
                                    return false;
                                }                            
                            } 
                        }
                    }
                    break;
                case 'base':
                    $opp=OrderProductBaseItem::where("order_product_package_id",$_op->id)
                        ->get();
                    foreach ($opp as $key=>$_item) {
                        if($_item->type==1){
                            $moo=MaintenanceOutOrder::where("product_type","base")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->get();
                            if(sizeof($moo)<=0){
                                return false;
                            }
                            $out_num=0;
                            foreach($moo as $_moo){
                                $out_num+=$_moo->number-$_moo->return_number;
                            }                            
                            if(bccomp($out_num,$_item->num,2)!=0){
                                return false;
                            } 
                        }
                    }
                    break;
                case 'service_card':
                    $opp=OrderProductScItem::where("order_product_id",$_op->id)
                        ->get();
                    foreach ($opp as $key=>$_item) {
                        if($_item->type==1){
                            $moo=MaintenanceOutOrder::where("product_type","service_card")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->get();
                            if(sizeof($moo)<=0){
                                return false;
                            }
                            $out_num=0;
                            foreach($moo as $_moo){
                                $out_num+=$_moo->number-$_moo->return_number;
                            }                            
                            if(bccomp($out_num,$_item->num,2)!=0){
                                return false;
                            } 
                        }
                    }
                    break;
                case 'non_standard':
                    $falg=true;
                    $opn=OrderProductNonStandard::where("order_product_id",$_op->id)->where('status',1)
                        ->get();
                    foreach ($opn as $key=>$value) {
                        //判断产品项是非标还是标准配件
                        if($value->service_type==1){
                            $moo=MaintenanceOutOrder::where("product_type","non_standard")->where("product_item_id",$value->id)->whereIn("status",array(0,1,2))->get();
                            if(sizeof($moo)<=0){
                                return false;
                            }
                            $out_num=0;
                            foreach($moo as $_moo){
                                $out_num+=$_moo->number-$_moo->return_number;
                            }
                            
                            if(bccomp($out_num,$value->num,2)!=0){
                                return false;
                            } 
                        }
                    }
                    break;
            }
        }
        return true;
    }
    /**
     * @Author:      zwh
     * @DateTime:    2016-6-22 13:53:01
     * @Description: 传入订单号检测该订单是否存在已出库的耗材
     */
    public static function isOutExist($order_id){
        $order_product=OrderProduct::where("order_id",$order_id)->where("status",1)->get();
        foreach ($order_product as $_op) {
            switch ($_op->product_type) {
                case 'enterprise':
                    $opep=OrderProductEsPackage::with("item")->where("order_product_id",$_op->id)->get();
                    foreach ($opep as $key => $value){
                        foreach ($value->item as $_item) {
                            if($_item->type==1){
                                $moo=MaintenanceOutOrder::where("product_type","enterprise")->where("product_item_id",$_item->id)->whereIn("status",array(1,2))->first();//查看有没有       备货中 等待领料 或者 已经领料的 备货申请 如果有 说明已经备货
                                if($moo){
                                    return false;
                                }
                            }
                        }
                    }
                    break;
                case 'activity':
                    $opacp=OrderProductAcPackage::with("item")->where("order_product_id",$_op->id)->get();
                    foreach ($opacp as $key => $value){
                        foreach ($value->item as $_item) {
                            if($_item->type==1){
                                $moo=MaintenanceOutOrder::where("product_type","activity")->where("product_item_id",$_item->id)->whereIn("status",array(1,2))->first();//查看有没有       备货中 等待领料 或者 已经领料的 备货申请 如果有 说明已经备货
                                if($moo){
                                    return false;
                                }
                            }
                        }
                    }
                    break;
                case 'promotion':
                    $opp=OrderProductPackage::with("item")->where("order_product_id",$_op->id)->get();
                    foreach ($opp as $key => $value){
                        foreach ($value->item as $_item) {
                            if($_item->type==1){
                                $moo=MaintenanceOutOrder::where("product_type","promotion")->where("product_item_id",$_item->id)->whereIn("status",array(1,2))->first();//查看有没有       备货中 等待领料 或者 已经领料的 备货申请 如果有 说明已经备货
                                if($moo){
                                    return false;
                                }
                            }
                        }
                    }
                    break;
                case 'base':
                    $opp=OrderProductBaseItem::where("order_product_package_id",$_op->id)
                    ->get();
                    foreach ($opp as $key=>$_item) {
                        if($_item->type==1){
                            $moo=MaintenanceOutOrder::where("product_type","base")->where("product_item_id",$_item->id)->whereIn("status",array(1,2))->first();
                            if($moo){
                                return false;
                            }
                        }
                    }
                    break;
                case 'non_standard':
                    $falg=true;
                    $opn=OrderProductNonStandard::where("order_product_id",$_op->id)->where('status',1)
                    ->get();
                    foreach ($opn as $key=>$value) {
                        //判断产品项是非标还是标准配件
                        if($value->service_type==1){
                            $moo=MaintenanceOutOrder::where("product_type","non_standard")->where("product_item_id",$value->id)->whereIn("status",array(1,2))->first();
                            if($moo){
                                return false;
                            }
                        }
                    }
                    break;
                case 'service_card':
                    $opp=OrderProductScItem::where("order_product_id",$_op->id)
                    ->get();
                    foreach ($opp as $key=>$_item) {
                        if($_item->type==1){
                            $moo=MaintenanceOutOrder::where("product_type","service_card")->where("product_item_id",$_item->id)->whereIn("status",array(1,2))->first();
                            if($moo){
                                return false;
                            }
                        }
                    }
                    break;
            }
        }
        return true;
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2017-1-24 10:40:18
     * @Description: 结算前判断出库数量是否一致
     */
    public static function outNum($order_id){
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>$order_id));
        $order_product=OrderProduct::where("order_id",$order_id)->where("status",1)->get();
        foreach ($order_product as $_op) {
            switch ($_op->product_type) {
                case 'enterprise':
                    $opep=OrderProductEsPackage::with("item")->where("order_product_id",$_op->id)->get();
                    foreach ($opep as $key => $value){
                        foreach ($value->item as $_item) {
                            if($_item->type==1){
                                $moo=MaintenanceOutOrder::where("product_type","enterprise")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->get();//查看有没有       备货中 等待领料 或者 已经领料的 备货申请 如果有 说明已经备货
                                if(sizeof($moo)<=0){
                                   
                                    $response = Lang::get("scs_error.order_out_return_notexit_40035");
                                    return $response;
                                }
                                $out_num=0;
                                foreach($moo as $_moo){
                                    $out_num+=$_moo->number-$_moo->return_number;
                                }
                                if(bccomp($out_num,$_item->num,2)!=0){
                                    // if($_item->item_type==1){
                                    //     $item=SupplyItemNonStandard::find($_item->item_id);
                                    //     $name=$item->model;
                                    // }else{
                                        $item=SupplyItem::find($_item->item_id);
                                        $name=$item->name;
                                    // }
                                    $response['code']=40076;
                                    $response["msg"]=$name." 商品实际出库数量为".$out_num."请调整后结算";
                                    return $response;
                                }
                            }
                        }
                    }
                    break;
                case 'activity':
                    $opacp=OrderProductAcPackage::with("item")->where("order_product_id",$_op->id)->get();
                    foreach ($opacp as $key => $value){
                        foreach ($value->item as $_item) {
                            if($_item->type==1){
                                $moo=MaintenanceOutOrder::where("product_type","activity")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->get();//查看有没有       备货中 等待领料 或者 已经领料的 备货申请 如果有 说明已经备货
                                if(sizeof($moo)<=0){
                                     
                                    $response = Lang::get("scs_error.order_out_return_notexit_40035");
                                    return $response;
                                }
                                $out_num=0;
                                foreach($moo as $_moo){
                                    $out_num+=$_moo->number-$_moo->return_number;
                                }
                                if(bccomp($out_num,$_item->num,2)!=0){
                                    // if($_item->item_type==1){
                                    //     $item=SupplyItemNonStandard::find($_item->item_id);
                                    //     $name=$item->model;
                                    // }else{
                                        $item=SupplyItem::find($_item->item_id);
                                        $name=$item->name;
                                    // }
                                    $response['code']=40076;
                                    $response["msg"]=$name." 商品实际出库数量为".$out_num."请调整后结算";
                                    return $response;
                                }
                            }
                        }
                    }
                    break;
                case 'promotion':
                    $opp=OrderProductPackage::with("item")->where("order_product_id",$_op->id)->get();
                    foreach ($opp as $key => $value){
                        foreach ($value->item as $_item) {
                            if($_item->type==1){
                                $moo=MaintenanceOutOrder::where("product_type","promotion")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->get();//查看有没有       备货中 等待领料 或者 已经领料的 备货申请 如果有 说明已经备货
                                if(sizeof($moo)<=0){
                                    $response = Lang::get("scs_error.order_out_return_notexit_40035");
                                     return $response;
                                }
                                $out_num=0;
                                foreach($moo as $_moo){
                                    $out_num+=$_moo->number-$_moo->return_number;
                                }
                                if(bccomp($out_num,$_item->num,2)!=0){
                                    // if($_item->item_type==1){
                                    //     $item=SupplyItemNonStandard::find($_item->item_id);
                                    //     $name=$item->model;
                                    // }else{
                                        $item=SupplyItem::find($_item->item_id);
                                        $name=$item->name;
                                    // }
                                    $response['code']=40076;
                                    $response["msg"]=$name." 商品实际出库数量为".$out_num."请调整后结算";
                                    return $response;
                                }
                            }
                        }
                    }
                    break;
                case 'base':
                    $opp=OrderProductBaseItem::where("order_product_package_id",$_op->id)
                    ->get();
                    foreach ($opp as $key=>$_item) {
                        if($_item->type==1){
                            $moo=MaintenanceOutOrder::where("product_type","base")->where("product_item_id",$_item->id)->whereIn("status",array(0,1,2))->get();
                            if(sizeof($moo)<=0){
                                $response = Lang::get("scs_error.order_out_return_notexit_40035");
                                return $response;
                            }
                            $out_num=0;
                            foreach($moo as $_moo){
                                $out_num+=$_moo->number-$_moo->return_number;
                            }
                            if(bccomp($out_num,$_item->num,2)!=0){
                                // if($_item->item_type==1){
                                //     $item=SupplyItemNonStandard::find($_item->item_id);
                                //     $name=$item->model;
                                // }else{
                                    $item=SupplyItem::find($_item->item_id);
                                    $name=$item->name;
                                // }
                                $response['code']=40076;
                                $response["msg"]=$name." 商品实际出库数量为".$out_num."请调整后结算";
                                return $response;
                            }
                        }
                    }
                    break;
                case 'non_standard':
                    $falg=true;
                    $opn=OrderProductNonStandard::where("order_product_id",$_op->id)->where('status',1)
                    ->get();
                    foreach ($opn as $key=>$value) {
                        //判断产品项是非标还是标准配件
                        if($value->service_type==1){
                            $moo=MaintenanceOutOrder::where("product_type","non_standard")->where("product_item_id",$value->id)->whereIn("status",array(0,1,2))->get();
                            if(sizeof($moo)<=0){
                                $response = Lang::get("scs_error.order_out_return_notexit_40035");
                                return $response;
                            }
                            $out_num=0;
                            foreach($moo as $_moo){
                                $out_num+=$_moo->number-$_moo->return_number;
                            }
                            if(bccomp($out_num,$value->num,2)!=0){
                                // if($value->type==1){
                                //     $item=SupplyItemNonStandard::find($value->item_id);
                                //     $name=$item->model;
                                // }else{
                                    $item=SupplyItem::find($value->item_id);
                                    $name=$item->name;
                                // }
                                $response['code']=40076;
                                $response["msg"]=$name." 商品实际出库数量为".$out_num."请调整后结算";
                                return $response;
                            }
                        }
                    }
                    break;
                case 'service_card':
                    $falg=true;
                    $opn=OrderProductScItem::where("order_product_id",$_op->id)->get();
                    foreach ($opn as $key=>$value) {
                        //判断产品项是非标还是标准配件
                        if($value->service_type==1){
                            $moo=MaintenanceOutOrder::where("product_type","service_card")->where("product_item_id",$value->id)->whereIn("status",array(0,1,2))->get();
                            if(sizeof($moo)<=0){
                                $response = Lang::get("scs_error.order_out_return_notexit_40035");
                                return $response;
                            }
                            $out_num=0;
                            foreach($moo as $_moo){
                                $out_num+=$_moo->number-$_moo->return_number;
                            }
                            if(bccomp($out_num,$value->num,2)!=0){
                                // if($value->type==1){
                                //     $item=SupplyItemNonStandard::find($value->item_id);
                                //     $name=$item->model;
                                // }else{
                                    $item=SupplyItem::find($value->item_id);
                                    $name=$item->name;
                                // }
                                $response['code']=40076;
                                $response["msg"]=$name." 商品实际出库数量为".$out_num."请调整后结算";
                                return $response;
                            }
                        }
                    }
                    break;
            }
        }
        return $response;
    }
}